package com.e3.basdoc.cust.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.e3.basdoc.cust.dao.CustDAO;
import com.e3.basdoc.cust.entity.CustAccountEntity;
import com.e3.basdoc.cust.entity.CustEntity;
import com.e3.basdoc.cust.model.CustAccountModel;
import com.e3.basdoc.cust.model.CustModel;
import com.e3.basdoc.cust.service.CustService;
import com.e3.core.base.DAOHelper;
import com.e3.core.base.ServiceException;
import com.e3.core.base.SuperService;
import com.e3.web.core.model.SuperModel;

public class CustServiceImpl extends SuperService implements CustService {

	private CustDAO custDAO;

	private CustDAO getCustDAO() {
		if (custDAO == null) {
			custDAO = DAOHelper.createDAOInstance(CustDAO.class);
		}
		return custDAO;
	}

	/*
	 * (non-Javadoc)
	 * @see com.e3.basdoc.cust.service.impl.CustService#newCust(com.e3.basdoc.cust.model.CustModel)
	 */
	public CustModel newCust(CustModel custModel) throws ServiceException {
		validateCust(custModel);
		checkCustExisted(custModel);

		CustEntity cust = custModelToEntity(custModel, null);
		custModel.setId( cust.getId());

		getCustDAO().persist(cust);

		return custModel;
	}

	/*
	 * (non-Javadoc)
	 * @see com.e3.basdoc.cust.service.impl.CustService#modifyCust(com.e3.basdoc.cust.model.CustModel)
	 */
	public CustModel modifyCust(CustModel custModel) throws ServiceException {
		validateCust(custModel);

		long id = custModel.getId();

		CustEntity entity = getCustDAO().getById(CustEntity.class, id);
		if (entity == null) {
			newCust(custModel);
			return custModel;
		}

		String oldCode = entity.getCode();
		if (!oldCode.equals(custModel.getCode())) {
			checkCustExisted(custModel);
		}

		custModelToEntity(custModel, entity);

		getCustDAO().merge(entity);

		return custModel;
	}

	private void validateCust(CustModel custModel) throws ServiceException {
		String code = custModel.getCode();
		String name = custModel.getName();

		if (code == null || code.trim().length() == 0 || name == null || name.trim().length() == 0) {
			throw new ServiceException("请设置公司的编码和名称。");
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.e3.basdoc.cust.service.impl.CustService#checkCustExisted(com.e3.basdoc.cust.model.CustModel)
	 */
	public void checkCustExisted(CustModel custModel) throws ServiceException {
		if (custModel == null) {
			return;
		}

		String code = custModel.getCode();
		String name = custModel.getName();
		CustEntity entity = getCustDAO().getCustByCode(code);
		if (entity != null) {
			throw new ServiceException("编码为'" + code + "',名称为'" + name + "'已经存在，请设置新的编码和名称。");
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.e3.basdoc.cust.service.impl.CustService#deleteCusts(java.util.List)
	 */
	public void deleteCusts(List<CustModel> custModels) throws ServiceException {
		if (custModels == null || custModels.isEmpty()) {
			return;
		}
		Long[] ids = getIds(custModels);
		getCustDAO().deleteCustByIds(ids);
		getCustDAO().deleteCustChildren(ids);
	}

	private Long[] getIds(List<? extends SuperModel> models) {
		if (models == null || models.isEmpty()) {
			return null;
		}
		Long[] ids = new Long[models.size()];
		for (int i = 0; i < models.size(); i++) {
			SuperModel model = models.get(i);
			ids[i] = model.getId();
		}
		return ids;
	}

	private CustEntity custModelToEntity(CustModel custModel, CustEntity entity) {
		CustEntity cust = super.ocModelToEntity(CustEntity.class, custModel, entity);

		// 基本信息
		cust.setCode(custModel.getCode());
		cust.setName(custModel.getName());
		cust.setShortName(custModel.getShortName());
		
		cust.setType(custModel.getType()==null?0:Integer.parseInt(custModel.getType()));

		cust.setParentId(custModel.getParentId());

		cust.setRegionId(custModel.getRegionId());

		cust.setBusinessAddress(custModel.getBusinessAddress());
		cust.setContactAddress(custModel.getContactAddress());
		cust.setTelphone(custModel.getTelphone());
		cust.setEmail(custModel.getEmail());
		cust.setZipcode(custModel.getZipcode());
		cust.setFax(custModel.getFax());
		cust.setRemark(custModel.getRemark());

		// 注册信息
		cust.setRegisterAddress(custModel.getRegisterAddress());
		cust.setLegalPerson(custModel.getLegalPerson());
		cust.setLegalPersonIdcard(custModel.getLegalPersonIdcard());
		cust.setRegisterNo(custModel.getRegisterNo());
		cust.setContactPerson(custModel.getContactPerson());
		cust.setContactWay(custModel.getContactWay());
		Date date = custModel.getFoundTime();
		if(date!=null){
			cust.setFoundTime(date.getTime());
		}
		date = custModel.getRegisterStartTime();
		if(date!=null){
			cust.setRegisterStartTime(date.getTime());
		}
		date = custModel.getRegisterEndTime();
		if(date!=null){
			cust.setRegisterEndTime(date.getTime());
		}
		cust.setCurrencyId(custModel.getCurrencyId());
		String taxType = custModel.getTaxpayerType();
		if(taxType!=null){
			cust.setTaxpayerType(Integer.parseInt(taxType));
		}

		// 其他信息
		List<CustAccountEntity> custAccountEntities = new ArrayList<CustAccountEntity>();
		List<CustAccountModel> custAccounts = custModel.getCustAccountList();
		for (CustAccountModel custAccount : custAccounts) {
			custAccountEntities.add(custAccountModelToEntity(custAccount, null));
		}
		cust.setCustAccounts(custAccountEntities);

		return cust;
	}

	private CustAccountEntity custAccountModelToEntity(CustAccountModel custAccountModel, CustAccountEntity custAccountEntity) {
		CustAccountEntity custAccount = super.ocModelToEntity(CustAccountEntity.class, custAccountModel, custAccountEntity);
		custAccount.setAccountIsDefault(custAccountModel.isAccountIsDefault());
		custAccount.setAccountType(custAccountModel.getAccountType());
		custAccount.setBankId(custAccountModel.getBankId());
		custAccount.setRemark(custAccountModel.getRemark());
		custAccount.setAccountNo(custAccountModel.getAccountNo());
		return custAccount;
	}

}
